//
//  AppDelegate.m
//  XSShortcutItem
//
//  Created by xingshuai on 2021/9/14.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //[self createItem];
    
    return YES;
}


// 桌面快捷方式启动app
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    // 判断从哪个桌面快捷键启动
    if ([shortcutItem.type isEqualToString:@"dashuaifirst"]) {
        NSString *str = [NSString stringWithFormat:@"用户从快捷方式<%@>进来的",shortcutItem.localizedTitle];
        [self creatAlertText:str];
    } else if ([shortcutItem.type isEqualToString:@"dashuaitwo"]) {
        NSString *str = [NSString stringWithFormat:@"用户从快捷方式<%@>进来的",shortcutItem.localizedTitle];
        [self creatAlertText:str];
    } else if ([shortcutItem.type isEqualToString:@"dashuaithree"]) {
        NSString *str = [NSString stringWithFormat:@"用户从快捷方式<%@>进来的",shortcutItem.localizedTitle];
        [self creatAlertText:str];
    }
}

/// 弹框测试
-(void)creatAlertText:(NSString *)str {
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
    [alter addAction:sure];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alter animated:YES completion:^{}];
}


/// 代码的方式创建桌面快捷选项，info.plist中添加或者代码创建都可以实现
-(void) createItem {
    UIApplicationShortcutIcon *icon0 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
    UIMutableApplicationShortcutItem *item0 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"dashuaifirst" localizedTitle:@"全网搜索" localizedSubtitle:nil icon:icon0 userInfo:nil];
    
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeTime];
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"dashuaitwo" localizedTitle:@"观看历史" localizedSubtitle:nil icon:icon1 userInfo:nil];
    
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"xz"];
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"dashuaithree" localizedTitle:@"离线缓存" localizedSubtitle:nil icon:icon2 userInfo:nil];

    NSArray *addArr = @[item0,item1,item2];
    [UIApplication sharedApplication].shortcutItems = addArr;
}




@end
